package cn.edu.nju.ws.sview.reasoning;

import java.util.ArrayList;

import cn.edu.nju.ws.sview.reasoning.provenance.Provenance;

/**
 * Rule
 * 
 * @author Saisai Gong
 * 
 */
public class Rule {
	private Atom<Term> head = null;
	private ArrayList<Atom<Term>> body = null;
	private Provenance provenance = null;
	
	/*特殊用途，本身并非Rule固有成员*/
	private ArrayList<Integer> rowIDs = null;
	private boolean flag;

	/**
	 * 构建rule
	 * 
	 * @param head
	 *            待构建的rule的head
	 * @param body
	 *            待构建的rule的body
	 */
	public Rule(Atom<Term> head, ArrayList<Atom<Term>> body,
			Provenance provenance) {
		this.head = head;
		this.body = body;
		this.provenance = provenance;
	}

	/**
	 * 
	 * @return 该rule的head
	 */
	public Atom<Term> getHead() {
		return this.head;
	}

	/**
	 * 
	 * @return 该rule的body
	 */
	public ArrayList<Atom<Term>> getBody() {
		return new ArrayList<Atom<Term>>(this.body);
	}

	/**
	 * 得到自定义customdatamember规则在数据库中对应的行
	 * @return
	 */
	public ArrayList<Integer> getRowIDs(){
		return this.rowIDs;
	}
	
	/**
	 * 设置自定义customdatamember规则在数据库中对应的行
	 * @return
	 */
	public void setRowIDs(ArrayList<Integer> rowIDs){
		this.rowIDs = rowIDs;
	}
	
	//取自定义customdatamember规则时判断取到规则是否是目标规则
	public boolean getFlag(){
		return this.flag;
	}
	//取自定义customdatamember规则时设置取到规则是否是目标规则
	public void setFlag(boolean flag){
		this.flag = flag;
	}
	
	/**
	 * 
	 * @return the rule provenance
	 */
	public Provenance getProvenance() {
		return this.provenance;
	}

	public boolean equals(Object rule) {
		if (rule instanceof Rule) {
			Rule ruleO = (Rule) rule;
			if (this.head.equals(ruleO.head) && this.body.equals(ruleO.body)) {
				return true;
			}
		}
		return false;
	}

	public int hashCode() {
		return this.head.hashCode() + this.body.hashCode();
	}
	
    public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(head);
		sb.append(":-");
		for (int i = 0; i < body.size(); i++) {
			sb.append(body.get(i));
			if (i != body.size() - 1)
				sb.append(", ");
		}
		sb.append(".");
		return sb.toString();
	}
}